package dao.impl;

import dao.UserDAO;
import pojo.User;

/**
 * @author CaiKe
 * @create 2021/1/8
 */
public class UserDAOImpl extends BaseDAO implements UserDAO {

    @Override
    public User queryUserByAccount(String account) {
        String sql = "select id, name, account, password, phone, gender, birth, address, email from t_user where account = ?";
        return queryForOne(User.class, sql, account);
    }

    @Override
    public User queryUserByAccountAndPassword(String account, String password) {
        String sql = "select id, name, account, password, phone, gender, birth, address, email from t_user where account=? and password=?";
        return queryForOne(User.class, sql, account, password);
    }

    @Override
    public Integer deleteUser(String account) {
        String sql = "delete from t_user where account = ?";
        return update(sql, account);
    }

    @Override
    public Integer updateUser(User user) {
        String sql = "update t_user set name=?, account=?, password=?, phone=?, gender=?, birth=?, address=?, email=? where account = ?";
        return update(sql, user.getName(), user.getAccount(), user.getPassword(), user.getPhone(), user.getGender(), user.getBirth(), user.getAddress(), user.getEmail(), user.getAccount());
    }

    @Override
    public Integer saveUser(User user) {
        String sql = "insert into t_user(name, account, password, phone, gender, birth, address, email) values(?,?,?,?,?,?,?,?)";
        return update(sql, user.getName(), user.getAccount(), user.getPassword(), user.getPhone(), user.getGender(), user.getBirth(), user.getAddress(), user.getEmail());
    }

    @Override
    public Integer updatePwd(String email, String password) {
        String sql = "update t_user set password=? where email=?";
        return update(sql, password, email);
    }
}